package com.easygem.front.service.imp;

import com.easygem.common.model.front.CallLog;
import com.easygem.common.model.front.ResourceInfo;
import com.easygem.common.model.system.User;
import com.easygem.common.response.ActiveUser;
import com.easygem.front.mapper.CallMapper;
import com.easygem.front.service.CallService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 廖思炜
 * @version 1.0
 * @className CallServiceImpl
 * @Description TODO
 * @date 2025/3/25 9:14
 */
@Service
public class CallServiceImpl implements CallService {
    @Autowired
    private CallMapper callMapper;
    /**
     * 根据账户名得到中间号
     *
     * @param userId
     * @return
     */
    @Override
    public String getBindNbrById(Long userId) {
        return callMapper.getBindNbrById(userId);
    }

    /**
     * 根据账户名得到应用key
     *
     * @param accountName
     * @return
     */
    @Override
    public String getAppIdByName(String accountName) {
        return callMapper.getAppIdByName(accountName);
    }

    /**
     * 根据账户名得到应用秘钥
     *
     * @param accountName
     * @return
     */
    @Override
    public String getAppSecretByName(String accountName) {
        return callMapper.getAppSecretByName(accountName);
    }

    /**
     * 根据账户名得到绑定唯一id
     *
     * @param accountName
     * @return
     */
    @Override
    public String getSessionId(String accountName) {
        return callMapper.getSessionId(accountName);
    }

    /**
     * 检查sessionId是否存在
     *
     * @param sessionId
     * @return
     */
    @Override
    public Integer checkSessionId(String sessionId) {
        return callMapper.checkSessionId(sessionId);
    }

    /**
     * 检查是否存在于资源并且有效
     *
     * @param phone
     * @return
     */
    @Override
    public Integer checkResource(String phone) {
        ActiveUser activeUser=(ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User user=activeUser.getUser();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("phone",phone);
        map.put("userId",user.getUserId());
        return callMapper.checkResource(map);
    }
    /**
     * 检查是否存在于资源并且有效b
     *
     * @param phone
     * @return
     */
    @Override
    public Integer checkResourceB(String phone) {
        ActiveUser activeUser=(ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User user=activeUser.getUser();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("phone",phone);
        map.put("userId",user.getUserId());
        return callMapper.checkResourceB(map);
    }
    /**
     * 保存日志
     *
     * @param callLog
     * @return
     */
    @Override
    public Integer saveLog(CallLog callLog) {
        return callMapper.saveLog(callLog);
    }

    /**
     * 话单推送回调
     *
     * @param callLog
     * @return
     */
    @Override
    public Integer callBackSaveLog(CallLog callLog) {
        return callMapper.callBackSaveLog(callLog);
    }

    /**
     * 根据用户得到当前用户的资费情况
     *
     * @param userId
     * @return
     */
    @Override
    public Double getTariffByUserId(Long userId) {
        return callMapper.getTariffByUserId(userId);
    }

    /**
     * 根据通话记录sessionId得到当前这一条记录的资费情况
     *
     * @param sessionId
     * @return
     */
    @Override
    public Double getTariffBySessionId(String sessionId) {
        return callMapper.getTariffBySessionId(sessionId);
    }

    /**
     * 将当前拨打号码的账户拨打号码数+1，将修改时间与最后呼叫时间更新
     *
     * @return
     */
    @Override
    public Integer updatedAccountCallNum() {
        ActiveUser activeUser=(ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User user=activeUser.getUser();
        Map<String,Object> map=new HashMap<>();
        map.put("accountName",user.getAccountName());
        map.put("lastUpdatedDate",Timestamp.valueOf(LocalDateTime.now()));
        map.put("lastCallTime", Timestamp.valueOf(LocalDateTime.now()));
        return callMapper.updatedAccountCallNum(map);
    }

    /**
     * 将当前拨打的资源更新是否拨打状态，将修改时间与最后呼叫时间更新
     *
     * @param map
     * @return
     */
    @Override
    public Integer updatedResourceCallInfo(Map<String, Object> map) {
        return callMapper.updatedResourceCallInfo(map);
    }

    /**
     * @param phoneA
     * @return
     */
    @Override
    public ResourceInfo selectResourceNameByPhoneA(String phoneA) {
        return callMapper.selectResourceNameByPhoneA(phoneA);
    }

    /**
     * @param phoneB
     * @return
     */
    @Override
    public ResourceInfo selectResourceNameByPhoneB(String phoneB) {
        return callMapper.selectResourceNameByPhoneB(phoneB);
    }
}
